package shark.picture.utils.other;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jianxin.wei
 * @description
 * @date 2025/8/29 10:41
 */
// 比对生产表和本地表字段
public class TableFieldComparator {
    public static void main(String[] args) {
        ExcelReader readerLocal = ExcelUtil.getReader("C:\\Users\\xiechang\\Desktop\\本地表字段.xlsx");
        ExcelReader readerPro = ExcelUtil.getReader("C:\\Users\\xiechang\\Desktop\\生产表字段.xlsx");
        List<List<Object>> readLocal = readerLocal.read();
        Map<String , Map<String,String>>  mapLocal = new HashMap<>();
        for (int i = 1; i < readLocal.size(); i++) {
            List<Object> read = readLocal.get(i);
            String tableName = (String) read.get(0);
            if (mapLocal.get(tableName) == null){
                Map<String,String> list = new HashMap<>();
                list.put(read.get(1).toString(),"数据类型："+read.get(2)
                        +(read.get(3) == null || read.get(3).toString().isEmpty() ? "":"，长度："+read.get(3).toString())
                        +(read.get(4) == null || read.get(4).toString().isEmpty() ? "":"，精度："+read.get(4).toString())
//                        +(read.get(5) == null ? "" : "，小数位："+read.get(5).toString())
                        +",是否为空："+read.get(6));
                mapLocal.put(tableName, list);
            }else {
                Map<String,String>  list = mapLocal.get(tableName);
                list.put(read.get(1).toString(),"数据类型："+read.get(2)
                        +(read.get(3) == null || read.get(3).toString().isEmpty() ? "":"，长度："+read.get(3).toString())
                        +(read.get(4) == null || read.get(4).toString().isEmpty() ? "":"，精度："+read.get(4).toString())
//                        +(read.get(5) == null ? "" : "，小数位："+read.get(5).toString())
                        +",是否为空："+read.get(6));
                mapLocal.put(tableName, list);
            }
        }
        List<List<Object>> readPro = readerPro.read();

        Map<String , Map<String,String>> mapPro = new HashMap<>();
        for (int i = 1; i < readPro.size(); i++) {
            List<Object> read = readPro.get(i);
            String tableName = (String) read.get(0);
            if (mapPro.get(tableName) == null){
                Map<String,String> list = new HashMap<>();
                list.put(read.get(1).toString(),"数据类型："+read.get(2)
                        +(read.get(3) == null || read.get(3).toString().isEmpty() ? "":"，长度："+read.get(3).toString())
                        +(read.get(4) == null || read.get(4).toString().isEmpty() ? "":"，精度："+read.get(4).toString())
//                        +(read.get(5) == null ? "" : "，小数位："+read.get(5).toString())
                        +",是否为空："+read.get(6));
                mapPro.put(tableName, list);
            }else {
                Map<String,String>list = mapPro.get(tableName);
//                list.put(read.get(1).toString(),read.get(2)+"-"+read.get(3)+"-"+read.get(4)+"-"+read.get(5)+"-"+read.get(6)+"-"+read.get(7));
                list.put(read.get(1).toString(),"数据类型："+read.get(2)
                        +(read.get(3) == null || read.get(3).toString().isEmpty() ? "":"，长度："+read.get(3).toString())
                        +(read.get(4) == null || read.get(4).toString().isEmpty() ? "":"，精度："+read.get(4).toString())
//                        +(read.get(5) == null ? "" : "，小数位："+read.get(5).toString())
                        +",是否为空："+read.get(6));
                mapPro.put(tableName, list);
            }
        }



        ArrayList<String> t1 = new ArrayList<>();
        ArrayList<String> t2 = new ArrayList<>();
        ArrayList<String> t3 = new ArrayList<>();
        ArrayList<String> t4 = new ArrayList<>();
        ArrayList<String> t5 = new ArrayList<>();
        ArrayList<String> t6 = new ArrayList<>();
        // 比对mapPro和 mapLocal不一样

        mapLocal.forEach((k,v) -> {
            if (mapPro.get(k) == null){
                t1.add(k);
            } else {
                for (Map.Entry<String, String> entry : v.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (!mapPro.get(k).containsKey(key)){
                        t2.add(StrUtil.format("生产表：{}  缺失字段：{}", k, key));
                    }else {
                        if (!mapPro.get(k).get(key).equals(value)){
                            t3.add(StrUtil.format("生产表：{}  字段不一致：{} ,\n  生产内容：{{}} , \n  本地内容：{{}}", k, key , mapPro.get(k).get(key) , value));
                        }
                    }
                }
            }
        });

        mapPro.forEach((k,v) -> {
            if (mapLocal.get(k) == null){
                t4.add(k);
            } else {
                for (Map.Entry<String, String> entry : v.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (!mapLocal.get(k).containsKey(key)){
                        t5.add(StrUtil.format("本地表：{}  缺失字段：{}", k, key));
                    }
                }
            }
        });


        System.out.println("生产中不存在该表：");
        t1.forEach( System.out::println);
        System.out.println("生产表缺失字段");
        t2.forEach( System.out::println);
        System.out.println("生产表字段内容不一致：");
        t3.forEach( System.out::println);
        System.out.println("本地中不存在该表：");
        t4.forEach( System.out::println);
        System.out.println("本地表缺失字段");
        t5.forEach( System.out::println);
    }
}
